package activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.lzp.testsmack.R;
import config.ServerInfo;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.muc.InvitationListener;
import org.jivesoftware.smackx.muc.MultiUserChat;

import utils.LogUtils;

public class MainActivity extends Activity {
    Connection connection = null;//服务器连接
    private Button btn_jump;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    initSmack();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        };
//        new MyAsyncTask(runnable).execute();
        btn_jump = (Button) findViewById(R.id.btn_jump);
        btn_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent showActivity = new Intent();
                showActivity.setClass(MainActivity.this,ShowActivity.class);
                startActivity(showActivity);
                LogUtils.ShowLog_d("start showActivity!");
            }
        });
    }
    class MyAsyncTask extends AsyncTask<Void,Void,Void> {
        private Runnable mRunnable;
        public MyAsyncTask(Runnable runnable){
            this.mRunnable = runnable;
        }


        @Override
        protected Void doInBackground(Void... params) {
            if(mRunnable!=null){
                mRunnable.run();
            }
            return null;
        }
    }
    //初始化服务器连接
    public void initSmack() throws XMPPException {
        Connection.DEBUG_ENABLED = false;
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(ServerInfo.ServerIP, ServerInfo.port, "");
        connectionConfiguration.setDebuggerEnabled(false);
        connectionConfiguration.setCompressionEnabled(false);//不启用压缩
        connection = new XMPPConnection(connectionConfiguration);
        connection.connect();
        String serviceName = connection.getServiceName();
        LogUtils.ShowLog_d("serviceName="+serviceName);
        connection.login("lzp", "123456");
        Presence presence = new Presence(Presence.Type.available);
        presence.setPriority(0);
        connection.sendPacket(presence);
        LogUtils.ShowLog_d("登陆成功！");
        registerPresenceFilter();
        registerInvitationListener();

    }

    /**
     * 接受聊天室邀请监听
     */
    private InvitationListener invitationListener;

    public void registerInvitationListener() {
        try {
            if (connection.isConnected()) {
                if (invitationListener == null) {
//                } else {
                    invitationListener = null;
                    invitationListener = new InvitationListener() {
                        @Override
                        public void invitationReceived(Connection connection, String room, String inviter, String reason, String password, Message message) {
                            LogUtils.ShowLog_d("接收到来自" + inviter + "的邀请 进入房间" + room + " 理由是" + reason + " 房间密码是" + password + " 消息" + message.getBody());
                        }
                    };
                }
                MultiUserChat.addInvitationListener(connection, invitationListener);
                LogUtils.ShowLog_d("注册聊天室监听成功");
            }
        } catch (Exception e) {
            LogUtils.ShowLog_d("注册聊天室监听失败");
        } finally {

        }
    }


    /**
     * 注册添加好友监听
     */
    private PacketListener PresenceListener;

    public void registerPresenceFilter() {
        if (PresenceListener != null) {
            connection.removePacketListener(PresenceListener);
        }
        PresenceListener = new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Presence presence = (Presence) packet;
                final String from = presence.getFrom();
                LogUtils.ShowLog_d("订阅请求 from = " + from);
                LogUtils.ShowLog_d("收到一个添加好友请求");
            }
        };

        //过滤器只接受订阅请求
        PacketFilter packetFilter = new PacketFilter() {
            @Override
            public boolean accept(Packet packet) {
                Presence presence = (Presence) packet;
                if (presence.getType().equals(Presence.Type.subscribe)) {
                    return true;
                }
                return false;
            }
        };
        AndFilter andFilter = new AndFilter(new PacketTypeFilter(Presence.class), packetFilter);
        connection.addPacketListener(PresenceListener, andFilter);
    }
}
